package com.bawei.mdm2207alast.report

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.mdm2207alast.base.OnError
import com.bawei.mdm2207alast.base.OnLoading
import com.bawei.mdm2207alast.base.OnSuccess
import com.bawei.mdm2207alast.common.BaseTitle
import com.bawei.mdm2207alast.model.AddReportEntity
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import kotlinx.coroutines.launch
import java.util.Date

/**
 * 作者：赵亮
 * 时间：2024/12/16 15:36
 * 功能：添加工作日志页面
 */
@Composable
fun AddReportPage(
    navController: NavController = rememberNavController(),
    json: String = "",
    viewModel: ReportViewModel = hiltViewModel()
) {

    val reportEntity = Gson().fromJson(json, ReportEntity::class.java)

    LaunchedEffect(key1 = "") {
        launch {
            viewModel.uiState.collect {
                when (it) {
                    is OnError -> {
                        ToastUtils.showShort(it.msg)
                    }

                    OnLoading -> {}
                    is OnSuccess<*> -> {
                        ToastUtils.showShort((it.data as AddReportEntity).msg)

                        // 返回上一个页面
                        navController.popBackStack()
                    }
                }
            }
        }
    }

    // 总结
    val zj by remember {
        mutableStateOf(
            listOf(
                "",
                "今日",
                "本周",
                "本月",
                "季度",
                "业绩",
                "营业"
            )[reportEntity.type]
        )
    }

    // 计划
    val jh by remember {
        mutableStateOf(
            listOf(
                "",
                "明日",
                "下周",
                "下月",
                "下季度",
                "下次业绩",
                "下次营业"
            )[reportEntity.type]
        )
    }

    // 总结输入框中的内容
    var zjEdit by remember {
        mutableStateOf("")
    }

    // 计划输入框中的内容
    var jhEdit by remember {
        mutableStateOf("")
    }


    Column {
        BaseTitle(navController = navController, title = "工作汇报")
        Column(
            modifier = Modifier.padding(top = 10.dp, start = 15.dp, end = 15.dp),
            verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            Text(text = reportEntity.title)
            Text(text = "$zj 工作总结")
            OutlinedTextField(
                modifier = Modifier.fillMaxWidth(),
                value = zjEdit, onValueChange = {
                    zjEdit = it
                })
            Text(text = "$jh 工作计划")
            OutlinedTextField(
                modifier = Modifier.fillMaxWidth(),
                value = jhEdit, onValueChange = {
                    jhEdit = it
                })
            Button(onClick = {

                val map = mapOf(
                    "reportType" to reportEntity.type,
                    "reportTime" to System.currentTimeMillis(),
                    "reportRemind" to "2207A",
                    "reportNowTitle" to "${zj}工作总结",
                    "reportNowValue" to zjEdit,
                    "reportNextTitle" to "${jh}工作计划",
                    "reportNextValue" to jhEdit
                )

                // 发送添加工作日志的意图
                viewModel.sendIntent(ReportIntent.AddReport(map))

            }, modifier = Modifier.fillMaxWidth()) {
                Text(text = "提交汇报")
            }
        }
    }
}

@Preview
@Composable
fun AddReportPagePreview() {
    AddReportPage()
}

